﻿var messageApp = {};

messageApp.messages = [];

//Read
messageApp.getMessages = function (success, error) {    
    var request = new XMLHttpRequest();

    request.open('GET', 'https://nicksmessages.firebaseio.com/messages/.json', true);
    request.onload = function () {
        //Good statuses
        if (this.status >= "200" && this.status < "400") {
            var data = JSON.parse(this.response);

            if (success && typeof (success) == "function") {
                success(data);
            }            
        }
        else {
            document.getElementById('result').innerHTML = "Some error occured";
        }
    }

    request.onerror = function () {
        error(this.status);
    }

    request.send();
}

var successCallback = function (data) {
    //Clear everything in the array
    messageApp.messages.length = 0;

    for (var m in data) {
        var message = data[m];
        message.id = m;
        messageApp.messages.push(message);
    }

    messageApp.displayTable();
}

var errorCallback = function (status) {
    alert(status);
}

//Get all the messages


messageApp.getMessages(successCallback, errorCallback);
//messageApp.getMessages(function () {
//    alert('I finished!');
//});

//Display
messageApp.displayTable = function () {
    var h = '<table>';
    h += '<tr>';
    h += '<th>Name</th>';
    h += '<th>Text</th>';
    h += '<th>Time</th>';
    h += '</tr>';

    for (var m in this.messages) {
        var message = this.messages[m];
        h += '<tr>';
        h += '<td>' + message.name + '</td>';
        h += '<td>' + message.text + '</td>';
        h += '<td>' + message.time + '</td>';
        h += '</tr>';
    }

    h += '</table>';

    document.getElementById('result').innerHTML = h;
}

//Create
messageApp.createMessage = function () {
    var nameInput = document.getElementById('Name');
    var textInput = document.getElementById('Text');

    var newMessage = {
        name: nameInput.value,
        text: textInput.value,
        time: new Date()
    };

    var request = new XMLHttpRequest();
    request.open('POST', 'https://nicksmessages.firebaseio.com/multi/.json', false);
    request.onload = function () {
        if (this.status >= "200" && this.status < "400") {
            var data = JSON.parse(this.response);
            newMessage.id = data.name;

            messageApp.messages.push(newMessage);
            messageApp.displayTable();
            //messageApp.getMessages();

            nameInput.value = '';
            textInput.value = '';
        }
        else {
            alert('Something failed!');
        }
    }

    console.log(newMessage);

    var newMessageAsString = JSON.stringify(newMessage);

    console.log(newMessageAsString);

    request.send(newMessageAsString);
}

messageApp.deleteMessage = function () {
    var deleteInput = document.getElementById('Delete');
    var index = deleteInput.value;

    var message = this.messages[index];

    var request = new XMLHttpRequest();
    request.open('DELETE', 'https://nicksmessages.firebaseio.com/messages/' + message.id + '.json', false);
    request.onload = function () {
        if (this.status == "200") {
            messageApp.messages.splice(index, 1);
            messageApp.displayTable();
        } else {
            console.log(this.status);
            console.log(this.response);
        }
    }

    request.send();
}